//
//  MeViewController.m
//  CarUser
//
//  Created by 王森林 on 2018/11/29.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "MeViewController.h"

#import "MineMainViewModel.h"
#import "MineMainTabHeaderView.h"
#import "MeInfoViewController.h"
#import "HomeCollectViewController.h"
#import "MePointViewController.h"
#import "MeOrderViewController.h"
#import "MeCircleViewController.h"
@interface MeViewController ()<TabviewViewModelDelegate,MineMainTabHeaderViewDelegate>

@property (nonatomic, strong) MineMainViewModel *viewmodel;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIImageView *headerImgv;
@property (nonatomic, strong) MineMainTabHeaderView *tabHeaderView;
@property (nonatomic, strong) UIButton *pushsettingBtn;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateHeadValue) name:NotificationCenterName_Fresh_Userinfo object:nil];
    
    
    self.viewmodel = [[MineMainViewModel alloc]init];
    self.viewmodel.delegate = self;
    self.viewmodel.childtab = self.tableview;
    self.viewmodel.childvc = self;
    
    //列表
    [self.view addSubview:self.tableview];
    self.tableview.scrollEnabled = NO;
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    //头部
    _tabHeaderView = [[MineMainTabHeaderView alloc]initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 180)];
    _tabHeaderView.backgroundColor = UIColor.clearColor;
    _tabHeaderView.delegate = self;
    self.tableview.tableHeaderView = _tabHeaderView;
    
    //底部
    self.headerImgv = [[UIImageView alloc]initWithImage:IMG(@"bg_title")];
    self.headerImgv.backgroundColor = Common_NavigroundColor;
    self.headerImgv.frame = CGRectMake(0, 0, BOUNDWIDTH, IS_IPHONE_X?(180 + 24):180);
    [self.view addSubview:self.headerImgv];
    [self.view insertSubview:self.headerImgv belowSubview:self.tableview];
    
    //设置按钮
    _pushsettingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_pushsettingBtn setImage:IMG(@"ic_my_setting") forState:UIControlStateNormal];
    _pushsettingBtn.frame = CGRectMake(0, 0, 20, 20);
    _pushsettingBtn.center = CGPointMake(BOUNDWIDTH - 15 - 10, (IS_IPHONE_X?56:32)+10);
    [_pushsettingBtn addTarget:self action:@selector(handleSetAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_pushsettingBtn];
    
    //数据

    [self updateHeadValue];
    
    [self.tableview reloadData];
    
    
    
}


- (void)handleSetAction:(id)sender
{
    
    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    
    MeInfoViewController *controller = [[MeInfoViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
    
    
}

- (void)updateHeadValue
{
    UserRes *user  = [CommonMethod getUser];
//    if (!user) {
//        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
//        return;
//    }
    [_tabHeaderView.headerImgv  sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL,user.USER.PHOTO1)] placeholderImage:IMG(@"ic_info_head")];

    
    if (user) {
         _tabHeaderView.pushBtn.enabled = NO;
        _tabHeaderView.nameLab.text = user.USER.NICK00;
       
        NSMutableString *str = [[NSMutableString alloc] initWithString:[user.mobile substringWithRange:NSMakeRange(3,5)]];
        _tabHeaderView.contentLab.text = [user.mobile stringByReplacingOccurrencesOfString:str withString:@"****" options:NSCaseInsensitiveSearch range:NSMakeRange(3,5)];

  
        
    } else {
        _tabHeaderView.pushBtn.enabled = YES;
        _tabHeaderView.nameLab.text = @"未登录";
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"登录/注册 " attributes:@{
                                                                                                                 NSFontAttributeName:_tabHeaderView.contentLab.font,
                                                                                                                 NSForegroundColorAttributeName:_tabHeaderView.contentLab.textColor
                                                                                                                 }];
        NSTextAttachment *imgattach = [[NSTextAttachment alloc]init];
        imgattach.image = IMG(@"ic_my_arrow_login");
        imgattach.bounds = CGRectMake(0, -2, 12, 12);
        NSAttributedString *attchStr = [NSAttributedString attributedStringWithAttachment:imgattach];
        [str appendAttributedString:attchStr];
        
        _tabHeaderView.contentLab.attributedText = str;
        
        
    }
    
}

#pragma mark ----------------------------------------------------------------------------------------------------MineMainTabHeaderViewDelegate
- (void)mineMainAction{
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
}


- (void)pushToOrderListWithStauts:(NSString *)status
{
    NSLog(@"%@",status);
    switch (status.integerValue) {
        case 1:
        {
            UserRes *user = [CommonMethod getUser];
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                return;
            }
            
            MeOrderViewController *controller = [[MeOrderViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 2:
        {
            UserRes *user = [CommonMethod getUser];
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                return;
            }
            HomeCollectViewController *controller = [[HomeCollectViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 3:
        {
            UserRes *user = [CommonMethod getUser];
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                return;
            }
            MePointViewController *controller = [[MePointViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
        case 4:
        {
            UserRes *user = [CommonMethod getUser];
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                return;
            }
            MeCircleViewController *controller = [[MeCircleViewController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:controller animated:YES];
            
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark ----------------------------------------------------------------------------------------------------scrollViewDidScroll
- (void)vm_scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    CGRect frame = CGRectMake(0, 0, BOUNDWIDTH, IS_IPHONE_X?(180 + 24):180);
    if (y > 0) {
        self.headerImgv.frame = ({
            frame.origin.y -= fabs(y);
            frame;
        });
    }else{
        if (fabs(y) > 64) {
            CGFloat sacle = 1 + (fabs(y) - 64) / (frame.size.height/2) ;
            self.headerImgv.transform = CGAffineTransformMakeScale(sacle, sacle);
        }else{
            self.headerImgv.transform = CGAffineTransformMakeScale(1, 1);
            self.headerImgv.frame = ({
                frame.size.height -= y;
                frame;
            });
        }
        
    }
}


/**
 初始化
 */
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.backgroundColor = UIColor.clearColor;
        _tableview.dataSource = self.viewmodel;
        _tableview.delegate = self.viewmodel;
        _tableview.tableFooterView = [UIView new];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.showsVerticalScrollIndicator = NO;
    }return _tableview;
}


@end
